home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / FromTheMag / JW FLV MEDIA PLAYER 4.2 / mediaplayer.exe / player.swf / scripts / com / jeroenwijering / models / VideoModel.as < prev    next >
Text File  |  2008-11-04  |  7KB  |  221 lines

  1. package com.jeroenwijering.models
  2. {
  3.    import com.jeroenwijering.events.ModelEvent;
  4.    import com.jeroenwijering.events.ModelStates;
  5.    import com.jeroenwijering.player.Model;
  6.    import com.jeroenwijering.utils.NetClient;
  7.    import flash.events.AsyncErrorEvent;
  8.    import flash.events.ErrorEvent;
  9.    import flash.events.IOErrorEvent;
  10.    import flash.events.NetStatusEvent;
  11.    import flash.events.SecurityErrorEvent;
  12.    import flash.media.SoundTransform;
  13.    import flash.media.Video;
  14.    import flash.net.NetConnection;
  15.    import flash.net.NetStream;
  16.    import flash.net.ObjectEncoding;
  17.    import flash.utils.clearInterval;
  18.    import flash.utils.setInterval;
  19.    
  20.    public class VideoModel implements ModelInterface
  21.    {
  22.        
  23.       
  24.       private var stream:NetStream;
  25.       
  26.       private var timeinterval:Number;
  27.       
  28.       private var connection:NetConnection;
  29.       
  30.       private var model:Model;
  31.       
  32.       private var loadinterval:Number;
  33.       
  34.       private var transform:SoundTransform;
  35.       
  36.       private var metadata:Boolean;
  37.       
  38.       private var video:Video;
  39.       
  40.       public function VideoModel(param1:Model)
  41.       {
  42.          super();
  43.          model = param1;
  44.          connection = new NetConnection();
  45.          connection.addEventListener(NetStatusEvent.NET_STATUS,statusHandler);
  46.          connection.addEventListener(SecurityErrorEvent.SECURITY_ERROR,errorHandler);
  47.          connection.objectEncoding = ObjectEncoding.AMF0;
  48.          connection.connect(null);
  49.          stream = new NetStream(connection);
  50.          stream.addEventListener(NetStatusEvent.NET_STATUS,statusHandler);
  51.          stream.addEventListener(IOErrorEvent.IO_ERROR,errorHandler);
  52.          stream.addEventListener(AsyncErrorEvent.ASYNC_ERROR,metaHandler);
  53.          stream.bufferTime = model.config["bufferlength"];
  54.          stream.client = new NetClient(this);
  55.          video = new Video(320,240);
  56.          video.attachNetStream(stream);
  57.          transform = new SoundTransform();
  58.          stream.soundTransform = transform;
  59.          quality(model.config["quality"]);
  60.          if(model.config["mute"] == true)
  61.          {
  62.             volume(0);
  63.          }
  64.          else
  65.          {
  66.             volume(model.config["volume"]);
  67.          }
  68.       }
  69.       
  70.       public function stop() : void
  71.       {
  72.          if(stream.bytesLoaded != stream.bytesTotal)
  73.          {
  74.             stream.close();
  75.          }
  76.          stream.pause();
  77.          metadata = false;
  78.          clearInterval(loadinterval);
  79.          clearInterval(timeinterval);
  80.       }
  81.       
  82.       private function loadHandler() : void
  83.       {
  84.          var _loc1_:* = undefined;
  85.          var _loc2_:* = undefined;
  86.          _loc1_ = stream.bytesLoaded;
  87.          _loc2_ = stream.bytesTotal;
  88.          model.sendEvent(ModelEvent.LOADED,{
  89.             "loaded":_loc1_,
  90.             "total":_loc2_
  91.          });
  92.          if(_loc1_ == _loc2_ && _loc1_ > 0)
  93.          {
  94.             clearInterval(loadinterval);
  95.          }
  96.       }
  97.       
  98.       private function metaHandler(param1:ErrorEvent) : void
  99.       {
  100.          model.sendEvent(ModelEvent.META,{"error":param1.text});
  101.       }
  102.       
  103.       private function timeHandler() : void
  104.       {
  105.          var _loc1_:* = undefined;
  106.          var _loc2_:* = undefined;
  107.          var _loc3_:* = undefined;
  108.          _loc1_ = Math.round(stream.bufferLength / stream.bufferTime * 100);
  109.          _loc2_ = Math.round(stream.time * 10) / 10;
  110.          _loc3_ = model.playlist[model.config["item"]]["duration"];
  111.          if(_loc1_ < 100 && _loc2_ < Math.abs(_loc3_ - stream.bufferTime * 2))
  112.          {
  113.             model.sendEvent(ModelEvent.BUFFER,{"percentage":_loc1_});
  114.             if(model.config["state"] != ModelStates.BUFFERING && _loc1_ < 10)
  115.             {
  116.                model.sendEvent(ModelEvent.STATE,{"newstate":ModelStates.BUFFERING});
  117.             }
  118.          }
  119.          else if(model.config["state"] == ModelStates.BUFFERING)
  120.          {
  121.             model.sendEvent(ModelEvent.STATE,{"newstate":ModelStates.PLAYING});
  122.          }
  123.          if(_loc3_ > 0)
  124.          {
  125.             model.sendEvent(ModelEvent.TIME,{
  126.                "position":_loc2_,
  127.                "duration":_loc3_
  128.             });
  129.          }
  130.       }
  131.       
  132.       private function statusHandler(param1:NetStatusEvent) : void
  133.       {
  134.          if(param1.info.code == "NetStream.Play.Stop" && stream.bytesLoaded == stream.bytesTotal)
  135.          {
  136.             clearInterval(timeinterval);
  137.             model.sendEvent(ModelEvent.STATE,{"newstate":ModelStates.COMPLETED});
  138.          }
  139.          else if(param1.info.code == "NetStream.Play.StreamNotFound")
  140.          {
  141.             stop();
  142.             model.sendEvent(ModelEvent.ERROR,{"message":"Video not found: " + model.playlist[model.config["item"]]["file"]});
  143.          }
  144.          model.sendEvent(ModelEvent.META,{"info":param1.info.code});
  145.       }
  146.       
  147.       public function volume(param1:Number) : void
  148.       {
  149.          transform.volume = param1 / 100;
  150.          stream.soundTransform = transform;
  151.       }
  152.       
  153.       private function errorHandler(param1:ErrorEvent) : void
  154.       {
  155.          model.sendEvent(ModelEvent.ERROR,{"message":param1.text});
  156.       }
  157.       
  158.       public function load() : void
  159.       {
  160.          model.mediaHandler(video);
  161.          stream.play(model.playlist[model.config["item"]]["file"]);
  162.          loadinterval = setInterval(loadHandler,100);
  163.          timeinterval = setInterval(timeHandler,100);
  164.          model.sendEvent(ModelEvent.STATE,{"newstate":ModelStates.BUFFERING});
  165.       }
  166.       
  167.       public function onData(param1:Object) : void
  168.       {
  169.          if(param1.type == "metadata" && !metadata)
  170.          {
  171.             metadata = true;
  172.             if(param1.width)
  173.             {
  174.                video.width = param1.width;
  175.                video.height = param1.height;
  176.             }
  177.             if(model.playlist[model.config["item"]]["start"] > 0)
  178.             {
  179.                seek(model.playlist[model.config["item"]]["start"]);
  180.             }
  181.          }
  182.          model.sendEvent(ModelEvent.META,param1);
  183.       }
  184.       
  185.       public function play() : void
  186.       {
  187.          stream.resume();
  188.          timeinterval = setInterval(timeHandler,100);
  189.          model.sendEvent(ModelEvent.STATE,{"newstate":ModelStates.PLAYING});
  190.       }
  191.       
  192.       public function seek(param1:Number) : void
  193.       {
  194.          clearInterval(timeinterval);
  195.          stream.seek(param1);
  196.          play();
  197.       }
  198.       
  199.       public function pause() : void
  200.       {
  201.          clearInterval(timeinterval);
  202.          stream.pause();
  203.          model.sendEvent(ModelEvent.STATE,{"newstate":ModelStates.PAUSED});
  204.       }
  205.       
  206.       public function quality(param1:Boolean) : void
  207.       {
  208.          if(param1 == true)
  209.          {
  210.             video.smoothing = true;
  211.             video.deblocking = 3;
  212.          }
  213.          else
  214.          {
  215.             video.smoothing = false;
  216.             video.deblocking = 1;
  217.          }
  218.       }
  219.    }
  220. }
  221.